package com.safeneeds.linguimana.entity.dao.impl;

import java.util.List;

import com.safeneeds.linguimana.dto.OndutyDataStorePort;
import com.safeneeds.linguimana.entity.PortGlutOnduty;
import com.safeneeds.linguimana.entity.dao.IPortGlutDutyDao;
import com.safeneeds.util.base.BaseHibernateDao;
import com.safeneeds.util.date.DateUtil;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;

public class PortGlutDutyDao extends BaseHibernateDao<PortGlutOnduty, String> implements IPortGlutDutyDao{

	/**
	 * 得到今日值班并封装到对象中
	 */
	@Override
	public List<PortGlutOnduty> getDuty() {
		String todayTime=DateUtil.getDateByYMD();
		return this.getEntities("from PortGlutOnduty where to_char(time,'yyyymmdd') = "+ todayTime);
	}

	@Override
	public PageInfo listOndutyPageDao(PageInfo pageInfo,
			OndutyDataStorePort ondutyDataStorePort) {
		// TODO Auto-generated method stub
		String hsql = "SELECT * FROM PORT_GLUT_ONDUTY ";
		String countSql = "SELECT COUNT(*) FROM PORT_GLUT_ONDUTY";
		
		String whereSql = "";
		String orderSql = " ORDER BY TIME desc";
		
		if(ondutyDataStorePort !=null){		
			whereSql += " where 1 = 1";
			//获取值班人名称
			if(!StringUtil.isEmpty(ondutyDataStorePort.getName())){
				whereSql += " and NAME like '%" + StringUtil.addQuote(ondutyDataStorePort.getName()) + "%'";
			}
			//获取带班人名称
			if(!StringUtil.isEmpty(ondutyDataStorePort.getLeader())){
				whereSql += " and LEADER like '%" + StringUtil.addQuote(ondutyDataStorePort.getLeader()) + "%'";
			}
			//时间
			if(!StringUtil.isEmpty(ondutyDataStorePort.getTimestart()) && !StringUtil.isEmpty(ondutyDataStorePort.getTimeend())){//有开始时间和结束时间
				if(DateUtil.compareDateYYYYMMDD(ondutyDataStorePort.getTimestart(), ondutyDataStorePort.getTimeend())){//开始时间大于结束时间
					whereSql += " and TIME >= to_timestamp('" + ondutyDataStorePort.getTimeend() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and TIME <= to_timestamp('" + ondutyDataStorePort.getTimestart() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
				}else{
					whereSql += " and TIME <= to_timestamp('" + ondutyDataStorePort.getTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and TIME >= to_timestamp('" + ondutyDataStorePort.getTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
				}
			}else if(!StringUtil.isEmpty(ondutyDataStorePort.getTimestart())){//只有开始时间
				whereSql += " and TIME >= to_timestamp('" + ondutyDataStorePort.getTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
			}else if(!StringUtil.isEmpty(ondutyDataStorePort.getTimeend())){//只有结束时间
				whereSql += " and TIME <= to_timestamp('" + ondutyDataStorePort.getTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
			}
		}
		pageInfo.setEntityClass(PortGlutOnduty.class);
		pageInfo.setListHbSql(hsql+whereSql+orderSql);
		pageInfo.setTotalCountHbSQL(countSql+whereSql);
		return pageInfo;
	}

	@Override
	public void addOndutyDao(PortGlutOnduty newonduty) {
		// TODO Auto-generated method stub
		this.saveEntity(newonduty);
	}

	@Override
	public PortGlutOnduty getOndutyByOndutyId(String ondutyId) {
		// TODO Auto-generated method stub
		return this.findById(ondutyId);
	}

	@Override
	public void saveOrUpadateOnduty(PortGlutOnduty newonduty) {
		// TODO Auto-generated method stub
		this.updateEntity(newonduty);
	}

	@Override
	public void deleteOndutyByIdDao(String ids) {
		// TODO Auto-generated method stub
		this.deleteEntityById(ids);
	}

	

}
